package com.bianguo.android.bprinter.entity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;

import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.bprinter.R;
import com.bianguo.android.bprinter.utils.RectUtil;

public class ZoomBean {

    private RectF mRect;
    private Bitmap deleteBitmap, mirrorBitmap, cutBitmap, scaleBitmap;
    private Matrix matrix, rectMatrix;
    private PointF deletePoint, mirrorPoint, scalePoint, cutPoint;
    private Bitmap srcBitmap;

    public ZoomBean(Context mContext, Matrix matrix) {
        deleteBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_delete_icon);
        mirrorBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_mirror_icon);
        cutBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_cut_icon);
        scaleBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zoom_scale_icon);
        this.matrix = matrix;
        rectMatrix = new Matrix();

        deletePoint = new PointF();
        mirrorPoint = new PointF();
        scalePoint = new PointF();
        cutPoint = new PointF();
    }

    public void setSrcBitmap(Bitmap bitmap) {
        this.srcBitmap = bitmap;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        deletePoint.x = -deleteBitmap.getWidth() / 2;
        deletePoint.y = -deleteBitmap.getHeight() / 2;

        mirrorPoint.x = width - mirrorBitmap.getWidth() / 2;
        mirrorPoint.y = -mirrorBitmap.getHeight() / 2;

        scalePoint.x = width- scaleBitmap.getWidth() / 2;
        scalePoint.y = height - scaleBitmap.getHeight() / 2;

        cutPoint.x = -cutBitmap.getWidth() / 2;
        cutPoint.y = height - cutBitmap.getHeight() / 2;

        mRect.left = 0;
        mRect.top = 0;
        mRect.right = width;
        mRect.bottom = height;
    }

    /**
     * 判断手指是否在图片区域内
     * @param x
     * @param y
     * @return
     */
    public boolean contains(float x, float y) {
        return x >= deletePoint.x && x <= scalePoint.x && y >= deletePoint.y && y <= scalePoint.y;
    }

    /**
     * 判断是否是否在单指缩放的图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsScale(float x, float y) {
        return x >= scalePoint.x && x <= scalePoint.x + scaleBitmap.getWidth() &&
                y >= scalePoint.y && y <= scalePoint.y + scaleBitmap.getHeight();
    }

    /**
     * 判断是否在镜像图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsMirror(float x, float y) {
        return x >= mirrorPoint.x && x <= mirrorPoint.x + mirrorBitmap.getWidth() &&
                y >= mirrorPoint.y && y <= mirrorPoint.y + mirrorBitmap.getHeight();
    }

    /**
     * 判断是否在删除图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsDelete(float x, float y) {
        return x >= deletePoint.x && x <= deletePoint.x + deleteBitmap.getWidth() &&
                y >= deletePoint.y && y <= deletePoint.y + deleteBitmap.getHeight();

    }

    /**
     * 判断是否在剪切图标上
     * @param x
     * @param y
     * @return
     */
    public boolean containsCut(float x, float y) {
        return x >= cutPoint.x && x <= cutPoint.x + cutBitmap.getWidth() &&
                y >= cutPoint.y && y <= cutPoint.y + cutBitmap.getHeight();
    }

    /**
     * 获取View的中心坐标
     *
     * @return
     */
    public PointF getCenterPoint() {
        PointF pointF = new PointF();
        pointF.x = (scalePoint.x + scaleBitmap.getWidth() / 2 + deletePoint.x + deleteBitmap.getWidth() / 2) / 2;
        pointF.y = (scalePoint.y + scaleBitmap.getHeight() / 2 + deletePoint.y + deleteBitmap.getHeight() / 2) / 2;
//        pointF.x = mRect.centerX();
//        pointF.y = mRect.centerY();
        return pointF;
    }

    public void drawRect(Canvas canvas, Paint mPaint) {
        canvas.drawRect(mRect, mPaint);
    }

    public void drawRect(Canvas canvas, Paint mPaint, float rotation) {
        canvas.save();
        PointF centerPoint = getCenterPoint();
        canvas.rotate(rotation, centerPoint.x, centerPoint.y);
        canvas.drawRect(mRect, mPaint);
        canvas.restore();
    }

    public void drawBitmap(Canvas canvas, Paint mPaint) {
        canvas.drawBitmap(srcBitmap, matrix, mPaint);
    }

    public void drawPoint(Canvas canvas, Paint mPaint) {
        //左上角
//        canvas.drawBitmap(deleteBitmap, mRect.left - deleteBitmap.getWidth() / 2, mRect.top - deleteBitmap.getHeight() / 2, null);
        canvas.drawBitmap(deleteBitmap, deletePoint.x, deletePoint.y, mPaint);
//        canvas.drawBitmap(deleteBitmap, deleteMatrix, mPaint);
        //右上角
//        canvas.drawBitmap(mirrorBitmap, mRect.right - mirrorBitmap.getWidth() / 2, mRect.top - mirrorBitmap.getHeight() / 2, null);
        canvas.drawBitmap(mirrorBitmap, mirrorPoint.x, mirrorPoint.y, mPaint);
//        canvas.drawBitmap(mirrorBitmap, mirrorMatrix, mPaint);
        //右下角
//        canvas.drawBitmap(scaleBitmap, mRect.right - scaleBitmap.getWidth() / 2, mRect.bottom - scaleBitmap.getHeight() / 2, null);
        canvas.drawBitmap(scaleBitmap, scalePoint.x, scalePoint.y, mPaint);
//        canvas.drawBitmap(scaleBitmap, scaleMatrix, mPaint);
        //左下角
//        canvas.drawBitmap(cutBitmap, mRect.left - cutBitmap.getWidth() / 2, mRect.bottom - cutBitmap.getHeight() / 2, null);
        canvas.drawBitmap(cutBitmap, cutPoint.x, cutPoint.y, mPaint);
//        canvas.drawBitmap(cutBitmap, cutMatrix, mPaint);
    }

    public RectF getRect() {
        return mRect;
    }

    public void setRect(RectF mRect) {
        this.mRect = mRect;
    }

    /**
     * 根据移动坐标更新View
     *
     * @param translateX
     * @param translateY
     */
    public void updatePoint(float translateX, float translateY) {
        deletePoint.x =  deletePoint.x + translateX;
        deletePoint.y =  deletePoint.y + translateY;

        mirrorPoint.x =  mirrorPoint.x + translateX;
        mirrorPoint.y =  mirrorPoint.y + translateY;

        scalePoint.x =  scalePoint.x + translateX;
        scalePoint.y =  scalePoint.y + translateY;

        cutPoint.x =  cutPoint.x + translateX;
        cutPoint.y =  cutPoint.y + translateY;

        updateRect();
    }

    /**
     * 根据缩放系数更新View
     *
     * @param scale
     */
    public void updatePoint(float scale) {

        float w = mRect.width();
        float h = mRect.height();

        float newW = scale * w;
        float newH = scale * h;

        float dx = (newW - w) / 2;
        float dy = (newH - h) / 2;

        mRect.left -= dx;
        mRect.top -= dy;
        mRect.right += dx;
        mRect.bottom += dy;

        updatePoint();
    }

    public void updatePoint(PointF centerPoint, float rotation) {
    }

    /**
     * 根据Point更新Rect
     */
    private void updateRect() {
        mRect.left = (int) (deletePoint.x + deleteBitmap.getWidth() / 2);
        mRect.right = (int) (scalePoint.x + scaleBitmap.getWidth() / 2);
        mRect.top = (int) (deletePoint.y + deleteBitmap.getHeight() / 2);
        mRect.bottom = (int) (scalePoint.y + scaleBitmap.getHeight() / 2);
    }

    /**
     * 根据Rect更新Point
     */
    private void updatePoint() {
        deletePoint.x = mRect.left - deleteBitmap.getWidth() / 2;
        deletePoint.y = mRect.top - deleteBitmap.getHeight() / 2;

        mirrorPoint.x = mRect.right - mirrorBitmap.getWidth() / 2;
        mirrorPoint.y = mRect.top - mirrorBitmap.getHeight() / 2;

        scalePoint.x = mRect.right - scaleBitmap.getWidth() / 2;
        scalePoint.y = mRect.bottom - scaleBitmap.getHeight() / 2;

        cutPoint.x = mRect.left - cutBitmap.getWidth() / 2;
        cutPoint.y = mRect.bottom - cutBitmap.getHeight() / 2;
    }

    public int getWidth() {
        return (int) mRect.width();
    }

    public int getHeight() {
        return (int) mRect.height();
    }
}
